﻿Imports System.ComponentModel
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports Sy.Data
Imports Sy.UI.Common

Public Class CProductInfo
    Inherits SqlObject(Of CProductInfo)
    Public Sub New(address As String, title As String, workplane As String, city As String, keyword As String, fromMap As String, fromUrl As String, phone As String, mobile As String, Optional area As String = "")
        Me.Address = address
        Me.Title = title
        Me.Workplane = workplane
        Me.City = city
        Me.Keyword = keyword
        Me.FromMap = fromMap
        Me.FromUrl = fromUrl
        Me.Phone = phone
        Me.mobile = mobile
        Me.Area = area
    End Sub
    Public Sub New()
    End Sub
    Public Overloads Function Add() As Boolean
        If LoadWhere(SQLkey.Address, Address).Length = 0 Then
            MyBase.Add()
            Return True
        Else
            Return False
        End If
    End Function
    <UIAdapter(Title:="关键词", Width:=95)>
    Public Property Keyword As String '关键词   '<UIAdapter(Title:="标题", Width:=95)>
    <UIAdapter("来源地图", Width:=70)>
    Public Property FromMap As String  '来源地图
    <UIAdapter("城市", Width:=70)>
    Public Property City As String  '城市
    <UIAdapter("区域", Width:=70)>
    Public Property Area As String  '区
    <UIAdapter("标题", Width:=150)>
    Public Property Title As String  '标题
    <UIAdapter("地址", Width:=200)>
    <SQLEnumKey(SQLkey.Address)>
    Public Property Address As String '地址
    <UIAdapter("坐标", Width:=95)>
    <SQLEnumKey(SQLkey.Workplane)>
    Public Property Workplane As String '坐标
    <UIAdapter("固定电话", Width:=95)>
    Public Property Phone As String '固定电话
    <UIAdapter("移动电话", Width:=95)>
    <SQLEnumKey(SQLkey.mobile)>
    Public Property mobile As String '移动电话
    <UIAdapter(False)>
    Private Property FromUrl As String ' 来源网址
    <UIAdapter(False)>
    Public Property Uid As String  '访问详情页所需Uid
    <UIAdapter(False)>
    Public Property Pid As String  '访问详情页所需PID
    Public Enum SQLkey
        Workplane
        mobile
        Address
    End Enum
    ' 不需要详情页的信息情况
    ' 其它信息从列表页中获取(标题名称，坐标, 地址，城市，关键字，数据源，坐标，来源网址, 座机，手机)
    Public Sub AppenDataTable(dt As DataTable, I As Integer)
        Dim dataRow As DataRow = dt.NewRow()
        Dim strs = UIAdapter.GetStringItems(Me, I) '状态 进度 id
        For n = 0 To strs.Length - 1
            dataRow(n) = strs(n)
        Next
        dt.Rows.Add(dataRow)
    End Sub
    Private Shared Function CreateDataTable() As DataTable
        Dim dataTable As New DataTable("CProductInfo")
        UIAdapter.StringsAdapter(Of CProductInfo)(
            Sub(str)
                dataTable.Columns.Add(New DataColumn(str, GetType(String)))
            End Sub, "编号")
        Return dataTable
    End Function
    Public Shared Function GetDataTable() As DataTable
        Dim dt As DataTable = CreateDataTable()
        Dim lst = Load().ToList
        For i = 0 To lst.Count - 1
            Dim m = lst(i)
            m.AppenDataTable(dt, i + 1)
        Next
        Return dt
    End Function
    Public Shared Function GetMobileDataTable() As DataTable
        Dim dt As New DataTable("PhoneNumber")
        dt.Columns.Add("手机号码")
        Dim lst = Load().ToList
        'Dim arr1 = lst.Select(Function(m As CProductInfo)
        '                          Dim mat = reg.Match($"{m.mobile},{m.phone}")
        '                          If mat.Success Then
        '                              Return mat.Groups(0).Value
        '                          Else Return Nothing
        '                          End If
        '                      End Function).Where(Function(m As String)
        '                                              Return m IsNot Nothing
        '                                          End Function).Distinct().ToArray '
        Dim PhoneLst As New List(Of String)
        For Each m In lst
            Dim phoneGroup = Phonereg.Match($"{m.Mobile},{m.Phone}")
            If phoneGroup.Success Then
                For Each g As Group In phoneGroup.Groups
                    If Not PhoneLst.Contains(g.Value) Then PhoneLst.Add(g.Value)
                Next
            End If
        Next
        For i = 0 To PhoneLst.Count - 1
            Dim m = PhoneLst(i)
            Dim dataRow As DataRow = dt.NewRow()
            If m.Length > 0 Then
                dataRow(0) = m
                dt.Rows.Add(dataRow)
            End If
        Next
        Return dt

    End Function
End Class
